package ge.devfest.hackathon.gg.model;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "itms_tool.db";
	private static final int DATABASE_VERSION = 5;
	
	private RuntimeExceptionDao<Image, Integer> imageDao;
	private RuntimeExceptionDao<Activity, Integer> activityDao;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Image.class);
			TableUtils.createTable(connectionSource, Activity.class);
			populateMockData();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, Image.class, true);
			TableUtils.dropTable(connectionSource, Activity.class, true);
			onCreate(database, connectionSource);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	private void populateMockData() {
	}
	
	public RuntimeExceptionDao<Image, Integer> getImageDao() {
		if (imageDao == null) {
			imageDao = getRuntimeExceptionDao(Image.class);
		}
		return imageDao;
	}
	
	public RuntimeExceptionDao<Activity, Integer> getActivityDao() {
		if (activityDao == null) {
			activityDao = getRuntimeExceptionDao(Activity.class);
		}
		return activityDao;
	}

}